//           Ascended International
//
//             Copyright (c) 2010
//
//            All Rights Reserved
// ------------------------------------------------------------------------------
//    * DO NOT REMOVE THIS HEADER. DO NOT MODIFY THIS HEADER *
/**********************************************************************************
 * You may use this class for non-commerical purposes. If you wish to use this    *
 * software for a commercial pupose please contact Ascended International at:     *
 * mark@ascended.com.au                                                           *
 *                                                                                *
 * When using this free class, no warranty express or implied is offered.         *
 * In no event shall Ascended International, any of it's employees or shareholds  *
 * be held liable for any direct, indirect, special, exemplary, incidental or     *
 * consequential damages however caused.                                          *
 *                                                                                *
 * If you modify this class, please add your name and the date below as a         *
 * contributor, without removing this notice or modifying it in any way, shape    *
 * or form.                                                                       *
 **********************************************************************************/

using System;

namespace Ascended.System
{
    public static class ConvertString
    {
        /// <summary>
        /// Converts the specified string representation of a number to an equivalent 32-bit signed integer.
        /// In order to make this class as fast as possible, it will not catch overflows.
        /// </summary>
        /// <param name="number">A string that contains a number to convert.</param>
        /// <returns>A 32-bit signed integer that is equivalent to the number in value, or 0 (zero) if value is null.</returns>
        public static int ToInt32(string number)
        {
            if (number == null || number == "")
            {
                return 0;
            }

            bool negative;
            int pos;

            int value = 0;


            if (number[0] == '-')
            {
                negative = true;
                pos = 1;
            }
            else
            {
                negative = false;
                pos = 0;
            }

            char c;
            int len = number.Length;

            for (; pos < len; pos++)
            {
                c = number[pos];

                if (c >= '0' && c <= '9')
                {
                    value = (value * 10) + (c - '0');
                }
                else if (c == ',')
                {
                    continue;
                }
                else
                {
                    break;
                }
            }

            return negative ? -value : value;
        }

    }
}
